package org.spiderflow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.spiderflow.common.controller.CURDController;
import org.spiderflow.core.mapper.FunctionMapper;
import org.spiderflow.core.model.Function;
import org.spiderflow.core.service.FunctionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zsh
 */
@RestController
@RequestMapping("/spider/function")
public class FunctionController extends CURDController<FunctionService, FunctionMapper, Function> {

    public FunctionController(FunctionService service) {
        super(service);
    }

    @RequestMapping("/list")
    public IPage<Function> list(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "limit", defaultValue = "1") Integer size, String name) {
        QueryWrapper<Function> select = new QueryWrapper<Function>().select("id", "name", "parameter", "create_date");
        if (StringUtils.isNotBlank(name)) {
            select.like("name", name);
        }
        select.orderByDesc("create_date");
        return service.page(new Page<>(page, size), select);
    }
}
